<template>
    <cloudAnalysis 
    :option="option" 
    v-if="loading" 
    @task="task" 
    @startDraw="startDraw" 
    @updateDraw="updateDraw"
    @groupChange="groupChange" 
    @fileUpload="fileUpload" 
    @changeFile="changeFile"
    />
</template>

<script>
import { deepClone } from '@/utils';
import { loadCloudToolPageRequest, submitCloudToolDrawTaskRequest, getFileGroupInfoRequest } from '@/api/tools'
export default {
    name: 'BaiaoweifanUiUserIndex',

    data() {
        return {
            option: {},
            loading: false,
            queryParams: {
                currentPage: 'prod',
                dataFileId: ['demo'],
                taskId: null,
                pageCode: null,
            },
            submitParams: {
                routeParams: null,
                pageCode: null,
                sessionId: localStorage.getItem('homeSessionId'),
                taskName: null,
            }
        };
    },
    created() {
        this.queryParams.taskId = this.$route.params.taskId || null
        this.init()
        this.subInit()
    },
    methods: {
        subInit() {
            this.submitParams.pageCode = this.$router.history.current.name
            this.submitParams.routeParams = this.$router.history.current.matched.reduce((pre, item, index) => {
                pre = pre + item.meta.title + ','
                return pre
            }, '')
        },
        async init() {
            this.queryParams.pageCode = this.$router.history.current.name
            const res = await loadCloudToolPageRequest(this.queryParams)
            if (res.code !== 200) return this.$message.error(res.message)
            this.loading = true
            this.option = deepClone(res.data)
        },
        //任务记录切换
        task(value) {
            this.queryParams.taskId = value
            this.init()
        },
        startDraw(form) {
            this.submitParams.analysisForm = form
        },
        async updateDraw(form) {
            this.submitParams.taskName = this.$taskTitle()
            this.submitParams.chartForm = form
            const res = await submitCloudToolDrawTaskRequest(this.submitParams)
            if (res.code !== 200) return this.$message.error(res.message)
            this.$message({ type: 'success', message: res.message })
        },
        groupChange(config, option) {
            this.option.analysisForm[config.code + config.type + '#'] = option
            if (config.type === 'Ygroup') return
            let num = -1
            const mutualExclusionOptions = option.group.dataGroupList.reduce((pre, item, index) => {
                if (item.ischecked) {
                    num++
                    pre.push({
                        value: num,
                        label: item.groupTitle
                    })
                }
                return pre
            }, [])
            for (let key in this.option.analysisForm) {
                if (key.includes('mutualExclusion#')) {
                    this.option.analysisForm[key] = mutualExclusionOptions
                }
            }
            for (let key in this.option.chartForm) {
                if (key.includes('mutualExclusion#')) {
                    this.option.chartForm[key] = mutualExclusionOptions
                }
            }
        },
        changeFile(id, code) {
            this.option.analysisForm[code + 'FileId#'] = id
        },
        async fileUpload(id) {
            const [fileId] = id
            const res = await getFileGroupInfoRequest({ fileId, pageCode: this.$router.history.current.name })
            if (res.code !== 200) return this.$message.error(res.message)
            for (let key in this.option.analysisForm) {
                for (let key1 in res.data) {
                    if (key === key1) {
                        this.option.analysisForm[key] = res.data[key1]
                    }
                }
            }
            for (let key in this.option.chartForm) {
                for (let key1 in res.data) {
                    if (key === key1) {
                        this.option.chartForm[key] = res.data[key1]
                    }
                }
            }
        },
    },
};
</script>